package zhaojin;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by asus on 2017/12/2.
 */
public class Backage {
    private int capacity;
    private int alreadytakeincapacity = 0;
    private List<Element> alreadytakein = new ArrayList<Element>();
    private boolean nowleafistakein;

    Backage(){
        this(0,null);
    }

    Backage(int capacity){
        this.capacity = capacity;
    }

    Backage(int capacity,List<Element> alreadypackin){
        this.capacity = capacity;
        this.alreadytakein = alreadypackin;
    }
    public List<Element> getAlreadytakein() {
        return alreadytakein;
    }

    public int getCapacity() {
        return capacity;
    }

    public boolean getin(Element element){
        if(alreadytakeincapacity+element.getWeight()<=capacity){
            alreadytakein.add(element);
            nowleafistakein = true;
            alreadytakeincapacity += element.getWeight();
            return true;
        }
        else{
            nowleafistakein = false;
            return false;
        }
    }

    public boolean isNowleafistakein() {
        return nowleafistakein;
    }

    public Element getAlreadytakeinlast() {
        return alreadytakein.get(alreadytakein.size()-1);
    }

    public void getLastElementOut(){
        alreadytakeincapacity -= alreadytakein.get(alreadytakein.size()-1).getWeight();
        alreadytakein.remove(alreadytakein.size()-1);
    }

    public int nowvalue(){
        int nowvalue = 0;
        for(Element element:alreadytakein){
            nowvalue += element.getValue();
        }
        return nowvalue;
    }

    public int getLastElementIdinBackage(){
        return alreadytakein.get(alreadytakein.size()-1).getId();
    }
}
